package me.thava.android.tagedit.mp3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Reads ID3V1 tags from mp3 files.
 * @author thavanathan
 *
 */
public class ID3V1 {
	private String title = ""; // 30 bytes
	private String artist = ""; // 30 bytes
	private String album = ""; // 30 bytes
	private String year = ""; // 4 bytes
	private String comment = ""; // 28 to 30 bytes
	private String genre = ""; // 1 byte index of genres.
	
	private File file = null;
	
	public ID3V1(File file) throws IOException{
		this.file = file;
		readTags();
	}
	
	private void readTags() throws IOException{
		FileInputStream fi = new FileInputStream(this.file);
		long file_size = this.file.length();
		fi.skip(file_size - 128);
		fi.skip(3); // skip TAG
		byte[] buffer = new byte[30];
		fi.read(buffer, 0, 30);
		this.title = new String(buffer, 0, 30).trim();
		fi.read(buffer, 0, 30);
		this.artist = new String(buffer, 0, 30).trim();
		fi.read(buffer, 0, 30);
		this.album = new String(buffer, 0, 30).trim();
		fi.read(buffer, 0, 4);
		this.year = new String(buffer, 0, 4).trim();
		fi.read(buffer, 0, 30);
		this.comment = new String(buffer, 0, 30).trim();
		fi.read(buffer, 0, 1);
		this.genre = new String(buffer, 0, 1).trim();
	}
	
	public static void main(String[] args) throws IOException{
		File f= new File("/home/thavanathan/Music/Chinna Kannan.mp3");
		ID3V1 id = new ID3V1(f);
		System.out.println("Title: " + id.title);
		System.out.println("Album: " + id.album);
		System.out.println("Artist: " + id.artist);
		System.out.println("Year: " + id.year);
		System.out.println("Comment: "+ id.comment);
		System.out.println("Genre: "+ id.genre);
	}
}
